﻿Option Explicit On

Imports System.Windows.Controls.Canvas

Public Class clsLine
    Public Event Move(ByVal sender As Object)

    Private WithEvents m_Line As Line
    Private WithEvents m_LHolder As clsHolder
    Private WithEvents m_RHolder As clsHolder

    Private m_Ancestor As shpConnector
    Private m_Key As String

#Region "properties"
    Public Property Shape() As Line
        Get
            Return m_Line
        End Get
        Set(ByVal value As Line)
            m_Line = value
        End Set
    End Property

    Public Property LeftHolder() As clsHolder
        Get
            Return m_LHolder
        End Get
        Set(ByVal value As clsHolder)
            m_LHolder = value
        End Set
    End Property

    Public Property RightHolder() As clsHolder
        Get
            Return m_RHolder
        End Get
        Set(ByVal value As clsHolder)
            m_RHolder = value
        End Set
    End Property

    Public ReadOnly Property Key() As String
        Get
            Key = m_Key
        End Get
    End Property

    Public ReadOnly Property Ancestor() As shpConnector
        Get
            Ancestor = m_Ancestor
        End Get
    End Property
#End Region

    Public Sub New(ByVal par As shpConnector, ByVal key As String)
        m_Key = key
        m_Ancestor = par
        m_Line = New Line
        m_Line.Stroke = ColorLine
        m_Line.StrokeThickness = c_Line
        m_Line.Visibility = Windows.Visibility.Visible
        m_Line.Focusable = True
    End Sub

    Public Sub MoveShape(Optional ByVal x1 As Integer = -1, Optional ByVal y1 As Integer = -1, Optional ByVal x2 As Integer = -1, Optional ByVal y2 As Integer = -1)
        If x1 > -1 Then
            m_Line.X1 = x1
        End If
        If y1 > -1 Then
            m_Line.Y1 = y1
        End If
        If x2 > -1 Then
            m_Line.X2 = x2
        End If
        If y2 > -1 Then
            m_Line.Y2 = y2
        End If
        RaiseEvent Move(Me)
    End Sub

    Public Function Neighbor(ByVal hol As clsHolder) As clsHolder
        Neighbor = Nothing
        If hol Is m_LHolder Then
            Neighbor = m_RHolder
        ElseIf hol Is m_RHolder Then
            Neighbor = m_LHolder
        End If
    End Function

    Private Sub LeftHolder_Move(ByVal sender As Object) Handles m_LHolder.Move
        Dim shp As Rectangle, x As Integer, y As Integer
        shp = m_LHolder.Shape
        x = shp.Width / 2 + GetLeft(shp) : y = shp.Height / 2 + GetTop(shp)
        m_Line.X1 = x
        m_Line.Y1 = y
    End Sub

    Private Sub RightHolder_Move(ByVal sender As Object) Handles m_RHolder.Move
        Dim shp As Rectangle, x As Integer, y As Integer
        shp = m_RHolder.Shape
        x = shp.Width / 2 + GetLeft(shp) : y = shp.Height / 2 + GetTop(shp)
        m_Line.X2 = x
        m_Line.Y2 = y
    End Sub

    Private Sub shpLine_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs) Handles m_Line.MouseLeftButtonDown
        m_Ancestor.Focus()
    End Sub

    Private Sub shpLine_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles m_Line.MouseMove
        m_Line.Cursor = Cursors.Hand
    End Sub
End Class
